package org.pp.config;

import jakarta.annotation.PostConstruct;
import lombok.extern.slf4j.Slf4j;
import org.pp.service.IPpOrderService;
import org.redisson.api.RQueue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class OrderDelayExcutor {
    private final RQueue<String> queue;

    public OrderDelayExcutor(RQueue<String> queue) {
        this.queue = queue;
    }

    @Autowired
    IPpOrderService orderService;

    @PostConstruct
    public void start() {
        new Thread(() -> {
            while (true) {
                try {
                    String orderNo = queue.poll();
                    if (orderNo != null) {
                        //取消订单的业务
                        log.info("自动取消订单:{}",orderNo);
                        orderService.sysCancal(orderNo);
                    } else {
                        Thread.sleep(50);
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }, "task-consumer-thread").start();
    }

}
